/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.mp.common.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.mp.common.model.WeixinKeywordresponse;
import com.yami.shop.mp.common.service.WeixinKeywordresponseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lgh on 2019/01/18.
 */
@RestController
@RequestMapping("/weixin/keywordresponse")
@Api(tags = "平台端微信关键词回复接口")
public class WeixinKeywordresponseController {

    @Autowired
    private WeixinKeywordresponseService weixinKeywordresponseService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:page')")
    @ApiOperation(value = "整页获取关键词回复")
    public ServerResponseEntity<List<WeixinKeywordresponse>> page() {
        List<WeixinKeywordresponse> list = weixinKeywordresponseService.listWeixinKeywordresponse();
        for (WeixinKeywordresponse weixinKeywordresponse : list) {
            if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
                weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
            }
        }
        return ServerResponseEntity.success(list);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:save')")
    @ApiOperation(value = "保存关键词回复")
    public ServerResponseEntity<Void> save(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse) {
        if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
            weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
        }
        weixinKeywordresponseService.save(weixinKeywordresponse);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:update')")
    @ApiOperation(value = "修改关键词回复")
    public ServerResponseEntity<Void> update(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse) {
        if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
            weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
        }
        weixinKeywordresponseService.updateById(weixinKeywordresponse);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:delete')")
    @ApiOperation(value = "根据id删除关键词回复")
    @ApiImplicitParam(name = "id", value = "关键词回复id", required = true, dataType = "Long")
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        weixinKeywordresponseService.removeById(id);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }
}
